$(function () {
    /*===========表单数据家载=================*/
    var val = $('#queryData').val();
    $('#apply_check').dialog({
        width:400,
        height:200,
        closed:'true',
        buttons:'#apply_check_buttons'

    })
    console.log(val)
    $.post('/insuranceInfoChange/approvalByInsurancePolicyNumber', {insurancePolicyNumber: val}, function (data) {
        console.log(data)
        if (data.orderbill) {
            console.log(data.orderbill.orderBillNumber);
            data['orderbill.orderBillNumber'] = data.orderbill.orderBillNumber;
        }
        if(data){
            data['auditinsurancetime000'] = data.auditinsurancetime;
            data['checkeffectivetime000'] = data.checkeffectivetime;
        }

        $('#approvalassign').val(data.approvalassign)

        if (data.employee) {
            data['employee.name'] = data.employee.name;
        }
        if (data.client) {
            data['client.realName'] = data.client.realName;
            data['client.idCardType'] = data.client.idCardType;
            data['client.clientType'] = data.client.clientType;
            data['client.idCardNumber'] = data.client.idCardNumber;
            data['client.applicant'] = data.client.applicant;
            data['client.applicant'] = data.client.applicant;
            data['client.nativePlace'] = data.client.nativePlace;
            data['client.tel'] = data.client.tel;
            data['client.address'] = data.client.address;
            console.log(data)
        }
        ;
        $('#all_form').form('load', data)
    })

    $.post('/insuranceInfoChange/approvalByInsurancePolicyNumber', {insurancePolicyNumber: val}, function (data) {
        if (data.client) {
            data['client.realName'] = data.client.realName;
            data['client.idCardType'] = data.client.idCardType;
            data['client.clientType'] = data.client.clientType;
            data['client.idCardNumber'] = data.client.idCardNumber;
            data['client.applicant'] = data.client.applicant;
            data['client.nativePlace'] = data.client.nativePlace;
            data['client.tel'] = data.client.tel;
            data['client.address'] = data.client.address;
            console.log(data)
        }
        ;
        $('#all_form').form('load', data)
    })
    var total;
    $.post('/insuranceInfoChange/approvalByInsurancePolicyNumber', {insurancePolicyNumber: val}, function (data) {
        if (data.orderbill) {
            data['orderbill.applyTime000'] = data.orderbill.applyTime;
            data['orderbill.beginTime000'] = data.orderbill.beginTime;
            data['orderbill.endTime000'] = data.orderbill.endTime;
            data['orderbill.coefficient'] = data.orderbill.coefficient;
            data['orderbill.id'] = data.orderbill.id;


            //==========================日期相减===============================
            var date1 = new Date(data.orderbill.beginTime)
            var date2 = new Date(data.orderbill.endTime)

            var s1 = date1.getTime(), s2 = date2.getTime();
            var total = (s2 - s1) / 1000;
            var day = parseInt(total / (24 * 60 * 60));//计算整数天数

            data['orderbill.totalTimes'] = day;
            //=========================================================
            console.log(data)
        }
        ;
        $('#all_form').form('load', data)
    })
    $.post('/insuranceInfoChange/approvalByInsurancePolicyNumber', {insurancePolicyNumber: val}, function (data) {
        if (data.clientcarinfo) {
            data['clientcarinfo.id'] = data. id;
            data['clientcarinfo.hasCarNumber'] = data.clientcarinfo.hasCarNumber
            data['clientcarinfo.newCar'] = data.clientcarinfo.newCar
            data['clientcarinfo.newCarPrice'] = data.clientcarinfo.newCarPrice
            data['clientcarinfo.foreignCar'] = data.clientcarinfo.foreignCar
            data['clientcarinfo.transferCar'] = data.clientcarinfo.transferCar
            data['clientcarinfo.carBrand'] = data.clientcarinfo.carBrand
            data['clientcarinfo.mainChassis'] = data.clientcarinfo.mainChassis
            data['clientcarinfo.carNumberType'] = data.clientcarinfo.carNumberType
            data['clientcarinfo.registerTime000'] = data.clientcarinfo.registerTime
            data['clientcarinfo.engineNumber'] = data.clientcarinfo.engineNumber
            data['clientcarinfo.carNumber'] = data.clientcarinfo.carNumber
            data['clientcarinfo.workingNature'] = data.clientcarinfo.workingNature
            data['clientcarinfo.workingArea'] = data.clientcarinfo.workingArea
            data['clientcarinfo.transferTime000'] = data.clientcarinfo.transferTime
            data['clientcarinfo.driverInfo'] = data.clientcarinfo.driverInfo
            data['clientcarinfo.relationToClient'] = data.clientcarinfo.relationToClient
            data['clientcarinfo.fleet'] = data.clientcarinfo.fleet
            data['clientcarinfo.queryNumber'] = data.clientcarinfo.queryNumber
            data['clientcarinfo.checkNumber'] = data.clientcarinfo.checkNumber
            data['clientcarinfo.workingNature'] = data.clientcarinfo.workingNature
            data['clientcarinfo.carTpye'] = data.clientcarinfo.carTpye
            data['clientcarinfo.importation'] = data.clientcarinfo.importation
            data['clientcarinfo.airRelease'] = data.clientcarinfo.airRelease
            data['clientcarinfo.power'] = data.clientcarinfo.power
            data['clientcarinfo.transferTime000'] = data.clientcarinfo.transferTime
        }
        if(data.carInfo){
            data['carInfo.airRelease'] = data.carInfo.airRelease
            data['carInfo.basesNumber'] = data.carInfo.basesNumber
            data['carInfo.carType'] = data.carInfo.carType;
            data['carInfo.importation'] = data.carInfo.importation
            data['carInfo.engineNumber'] = data.carInfo.engineNumber
            data['carInfo.id'] = data.carInfo.id
            data['carInfo.mainChassis'] = data.carInfo.mainChassis
            data['carInfo.newCarPrice'] = data.carInfo.newCarPrice
            data['carInfo.power'] = data.carInfo.power
            data['carInfo.rateDuality'] = data.carInfo.rateDuality
            data['carInfo.seatNumber'] = data.carInfo.seatNumber
            data['carInfo.sn'] = data.carInfo.sn


        }
        $('#all_form').form('load', data)
    })
    // $('#form_applyClient').form('load','/insuranceInfoChange/approvalByInsurancePolicyNumber')
    /*  $.get('/insuranceInfoChange/queryClientCar',{},function (data) {
          $('#form_clientCarInfo').form('load',data)
      })*/


    //======================日期计算===============================
    $('#end').datebox({
        onSelect: function (date) {
            var begainTime = $('#begaintime').combobox('getValue');
            var date1 = new Date(begainTime)
            var date2 = new Date(date)
            var s1 = date1.getTime(), s2 = date2.getTime();
            var total = (s2 - s1) / 1000;
            var day = parseInt(total / (24 * 60 * 60));//计算整数天数
            console.log(day)
            $('#totalTime').numberbox('setValue', day);
        }
    })

    //*********投保向导页面的事件*************
    $("#organization_id").combobox({
        onChange: function () {
            var id = $("#organization_id").combobox("getValue");

            $("#product_id").combobox({
                onBeforeLoad: function (param) {
                    param.orgId = id;
                }
            })
            /*$("#product_id").combobox("reload")*/
        }
    })
    //*********统一定义所有的panel面版*************
    $(".orderBill_panel00").panel({})


    //同被保人按钮  选中后将被保人信息填写框 先清除该表单的数据 再隐藏
    $("#panel_btn1").click(function () {
        var selected = $("#panel_btn1").prop("checked")
        if (selected) {
            // console.log($("#hidden_div1").prop("style").display);
            $("#form_recognizee").form("clear")
            $("#hidden_div1").prop("style").display = "none";
        } else {
            $("#hidden_div1").prop("style").display = "block";
        }
    })
    //同车主按钮  选中后将车主信息填写框 先清除该表单的数据 再隐藏
    $("#panel_btn2").click(function () {
        var selected = $("#panel_btn2").prop("checked")
        if (selected) {
            // console.log($("#hidden_div1").prop("style").display);
            $("#form_carOwner").form("clear")
            $("#hidden_div2").prop("style").display = "none";
        } else {
            $("#hidden_div2").prop("style").display = "block";
        }
    })
    //查询客户信息是的弹窗
    $("#clientinfo_dialog").dialog({
        title: 'My Dialog',
        width: 1200,
        height: 600,
        closed: true,
        /*href: 'client',*/
        /*content: '<iframe src="/orderBill/searchcllientinfo" width="100%" height="99%" frameborder="no"></iframe>',*/
        buttons: '#dialog_buttons'

    })

    //查询车辆信息是的弹窗
    $("#carinfo_dialog").dialog({
        title: 'My Dialog',
        width: 1200,
        height: 620,
        closed: true,
        /*href: 'client',*/
        /*content: '<iframe src="/orderBill/searchcllientinfo" width="100%" height="99%" frameborder="no"></iframe>',*/
        buttons: '#carInfo_dialog_buttons'

    })
    //客户信息查询弹窗中的表格-------------------------------------------------
    $(".client_datagrid").datagrid({
        url: '/sourceClient/list',
        fit: true,
        fitColumns: true,
        toolbar: '#datagrid_toolar',
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {field: 'clientType', title: '客户类型', width: 100},
            {field: 'username', title: '账号名', width: 60},
            {field: 'realName', title: '客户姓名', width: 100},
            {field: 'tel', title: '手机号', width: 100},
            {field: 'qq', title: 'QQ', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {field: 'address', title: '地址', width: 100},
            {field: 'idCardType', title: '证件类型', width: 100},
            {field: 'idCardNumber', title: '证件号码', width: 100},
            {field: 'job', title: '职业', width: 100},
            {field: 'clientNumber', title: '客户代码', width: 100},
            {field: 'clientLevel', title: '客户级别', width: 100}
        ]]
    });
    //车辆信息查询弹窗中的表格-------------------------------------------------
    $("#carinfo_datagrid").datagrid({
        url: '/car/list',
        fit: true,
        fitColumns: true,
        toolbar: '#carInfo_datagrid_toolar',
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {field: 'sn', title: '车型编码', width: 60},
            {field: 'carType', title: '车型分类', width: 100},
            {field: 'basesNumber', title: '核定载人', width: 100},
            {field: 'airRelease', title: '排气量', width: 100},
            {field: 'seatNumber', title: '座位数', width: 100},
            {field: 'rateDuality', title: '额定载质量(KG)', width: 100},
            {field: 'mainChassis', title: '车架号', width: 100},
            {field: 'engineNumber', title: '发动机号', width: 100},
            {field: 'newCarPrice', title: '新车购置价(元)', width: 100},
            {field: 'importation', title: '国产/进口', width: 100},
            {field: 'power', title: '功率', width: 100}
        ]]
    });

});
//************************************************以下都是自定义的方法****************************************************
//panel页面的确认按钮 点击之后跳转至明细编辑页面
/*function panel_ok() {
    $("#all_form").form("submit", {
        url: '/orderBill/save',
        success: function (data) {
            data = $.parseJSON(data)
            alert(data.success);
            if (data.success) {
                console.log(data.success);
                window.location.href = '/orderBill/orderBillItem?orgId=' + data.msg
            }
        }
    })
    //window.location.href='/orderBill/orderBillItem'
}*/


//=================保险限期计算====================
function total() {

}


//客户信息查询按钮  弹出客户信息页面
function client_search1() {
    $("#clientinfo_dialog").dialog("open")
}

//被保人查询按钮  弹出客户信息页面
function recognizee_search() {

    $("#clientinfo_dialog").dialog("open")
}

//车主信息查询按钮  弹出客户信息页面
function carOwner_search() {
    $("#clientinfo_dialog").dialog("open")
}

//车辆信息查询按钮  弹出车辆信息页面
function carInfo_search() {
    $("#carinfo_dialog").dialog("open")
}

//投保申请人信息确认  同时查询数据库 如果该客户存在就不保存 不存在就保存
function form_applyClient_check() {
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    //直接提交表单
    $("#form_applyClient").form("submit", {
        queryParams: {orderBillId: orderBillId},
        url: '/sourceClient/clientClick',
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert("温馨提示", data.msg, "info")
            } else {
                $.messager.alert("温馨提示", data.msg, "error")
            }
        }
    })

}

//投保申请人信息重置
function form_applyClient_reset() {
    $("#all_form").form("reset")
}

//被保人信息确认  同时查询数据库 如果该客户存在就不保存 不存在就保存
/*function form_recognizee_check(){
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    //直接提交表单
    $("#form_recognizee").form("submit",{
        queryParams:{orderBillId:orderBillId},
        url:'/sourceClient/clientClick',
        success:function(data){
            data = $.parseJSON(data);
            if (data.success){
                $.messager.alert("温馨提示",data.msg,"info")
            }else {
                $.messager.alert("温馨提示",data.msg,"error")
            }
        }
    })

}*/
//======================================================================
//被保人信息页的页脚的按钮
function form_recognizee_check() {
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    var applyIdCard = $("#idCardNumber01").val();
    if (applyIdCard) {
        //直接提交表单
        $("#all_form").form("submit", {
            queryParams: {orderBillId: orderBillId},
            url: '/sourceClient/clientClick',
            success: function (data) {
                data = $.parseJSON(data);
                if (data.success) {
                    $.messager.alert("温馨提示", data.msg, "info")
                } else {
                    $.messager.alert("温馨提示", data.msg, "error")
                }
            }
        })
    } else {
        $.messager.alert("温馨提示", "请先确认投保人信息", "info")
    }

}

//被保人信息重置
function form_recognizee_reset() {
    $("#all_form").form("reset")
}

//=======================================================================
//车主信息页的页脚的按钮
function form_carOwner_check() {
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    var applyIdCard = $("#idCardNumber02").val();
    if (applyIdCard) {
        //直接提交表单
        $("#all_form").form("submit", {
            queryParams: {orderBillId: orderBillId},
            url: '/sourceClient/clientClick',
            success: function (data) {
                data = $.parseJSON(data);
                if (data.success) {
                    $.messager.alert("温馨提示", data.msg, "info")
                } else {
                    $.messager.alert("温馨提示", data.msg, "error")
                }
            }
        })
    } else {
        $.messager.alert("温馨提示", "请先确认被保人信息", "info")
    }

}

//车主人信息重置
function form_carOwner_reset() {
    $("#all_form").form("reset")
}

//======================================================================
//客户查找弹窗的确认按钮 点击确认 将选择的信息回显
function dialog_ok() {
    var selectedRow = $(".client_datagrid").datagrid("getSelected");
    if (selectedRow) {
        //捕捉投保人身份证是否填写
        var applyIdCard = $("#idCardNumber01").val();
        if (applyIdCard) {
            //捕捉被保人身份证是否填写
            var applyIdCard2 = $("#idCardNumber02").val();
            if (applyIdCard2) {
                $("#all_form").form("load", selectedRow)
                $("#clientinfo_dialog").dialog("close")
            } else {
                $("#all_form").form("load", selectedRow)
                $("#clientinfo_dialog").dialog("close")
            }

        } else {
            $("#all_form").form("load", selectedRow)
            $("#clientinfo_dialog").dialog("close")
        }

    } else {
        $.messager.alert("温馨提示", "请选择客户信息", "info")
    }
}

//客户查找弹窗的返回按钮 点击返回 将关闭窗口
function dialog_cancel() {
    $("#clientinfo_dialog").dialog("close")
}

//车辆信息查找弹窗的确认按钮 点击确认 将选择的信息回显
function carInfo_dialog_ok() {
    var selectedRow = $("#carinfo_datagrid").datagrid("getSelected");
    if (selectedRow) {
        $("#form_clientCarInfo").form("load", selectedRow)

        $("#carinfo_dialog").dialog("close")
    } else {
        $.messager.alert("温馨提示", "请选择客户信息", "info")
    }
}

//客户查找弹窗的返回按钮 点击返回 将关闭窗口
function carInfo_dialog_cancel() {
    $("#carinfo_dialog").dialog("close")
}

//投保人信息填写完成后进行客户信息确认 将信息保存到数据库中
function client_submit() {
    var orderBillId = $("[id=id]").val();
    var applyIdCard1 = $("#idCardNumber01").val();
    var applyIdCard2 = $("#idCardNumber02").val();
    var applyIdCard3 = $("#idCardNumber03").val();
    //提交时间信息和基本信息
    $("#form_timeandbaseinfo").form("submit", {
        queryParams: {id: orderBillId, applyClient: applyIdCard1, recognizee: applyIdCard2, carOwner: applyIdCard3},
        url: '/orderBill/updateTimeAndBaseInfo'
    })
    //提交车辆信息和车辆附加信息
    $("#form_clientCarInfo").form("submit", {
        queryParams: {orderBillId: orderBillId},
        url: '/clientCarInfo/saveClientCarInfo'
    })
    //提交账户信息信息和车辆附加信息
    $("#form_accountInfo").form("submit", {
        queryParams: {applyClient: applyIdCard1},
        url: '/accountInfo/saveAccountInfo'
    })
    /* $("#form_applyClient").form("submit",{
     url:'employee',
     onSubmit: function(param){
     param.recognizee = 'value1'; //捕捉被保人身份证号码
     param.carOwner = 'value2';//捕捉车主身份证号码
     }
     })*/
}

function save() {
    var applyTime_fullYear = new Date($("#ET").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#ET").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#ET").datebox("getValue")).getDate();
    var effectTime = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;


    var applyTime_fullYear = new Date($("#AAT").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#AAT").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#AAT").datebox("getValue")).getDate();
    var auditinsurancetime = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;

    var applyTime_fullYear = new Date($("#AT").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#AT").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#AT").datebox("getValue")).getDate();
    var applyTime = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;


    var applyTime_fullYear = new Date($("#begaintime").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#begaintime").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#begaintime").datebox("getValue")).getDate();
    var begaintime = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;

    var applyTime_fullYear = new Date($("#end").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#end").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#end").datebox("getValue")).getDate();
    var end = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;

    var applyTime_fullYear = new Date($("#transferTime").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#transferTime").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#transferTime").datebox("getValue")).getDate();
    var transferTime = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;


    var applyTime_fullYear = new Date($("#registerTime").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#registerTime").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#registerTime").datebox("getValue")).getDate();
    var registerTime = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;



    $('#all_form').form('submit', {
        url: '/insuranceInfoChange/updateInsuranceWithOrderBill',
        onSubmit: function(param){
            param['checkeffectivetime']=effectTime;
            param['auditinsurancetime']=auditinsurancetime;
            param['orderbill.applyTime']=applyTime;
            param['orderbill.beginTime']=begaintime;
            param['orderbill.endTime']=end;
            param['clientcarinfo.registerTime']=registerTime;
            param['clientcarinfo.transferTime']=transferTime;

        },
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert('温馨提示', data.msg, 'info', function () {
                    $('#form_applyClient').form('clear')
                })
            } else {
                $.messager.alert('温馨提示', data.msg, "error")
            }
        }
    });
}
function apply_Check(){
    var val = $('#queryData').val();
    $.messager.confirm('确认','您确认想要提核吗？',function(yes){
            if(yes){
                $.post('orderBill/applyCheck',{insurancePolicyNumber:val},function (data) {
                    if(data.success){
                        $.messager.alert('温馨提示',data.msg,"info",function () {

                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg,"warning");
                    }
                })
            }
    });

}
function spendCal(){
    var msg;
    var val = $('#queryData').val();
    $.get('/orderBill/querySpend',{insurancePolicyNumber:val},function (data) {
        $.messager.show({
            title:'保费计算',
            msg:'原保费:'+data+'   现保费'+data+'    变化'+(data-data),
            showType:'show',
            style:{
                right:'',
                top:document.body.scrollTop+document.documentElement.scrollTop,
                bottom:''
            }
        });
    })
}



